package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping("/detail")
    public String getUserDetail(@RequestParam String username, Model model) {
        User user = userService.getUserWithRolesByUsername(username);
        model.addAttribute("user", user);
        return "user/detail";
    }

    @GetMapping("/search")
    public String searchUsers(
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String email,
            @RequestParam(required = false) Integer status,
            Model model) {
        List<User> users = userService.searchUsersWithRolesAndMenus(username, email, status);
        model.addAttribute("users", users);
        model.addAttribute("username", username);
        model.addAttribute("email", email);
        model.addAttribute("status", status);
        return "user/search";
    }

    @GetMapping("/list")
    public String userList(Model model) {
        List<User> users = userService.getAllUsers();
        model.addAttribute("users", users);
        return "user/list";
    }

    // 新增：完整用户详情页面
    @GetMapping("/fullDetail")
    public String getUserFullDetail(@RequestParam String username, Model model) {
        User user = userService.getUserDetailWithRolesAndMenus(username);
        model.addAttribute("user", user);
        return "user/fullDetail";
    }
}